var e = require("../../../@babel/runtime/helpers/interopRequireDefault").default, t = require("../../../@babel/runtime/helpers/slicedToArray"), n = require("../../../@babel/runtime/helpers/regeneratorRuntime"), o = require("../../../@babel/runtime/helpers/asyncToGenerator"), a = e(require("../../../api/sendcoupon.js")), i = e(require("@vant/weapp/dialog/dialog.js")), r = e(require("../../../kta/index")), c = require("../../../utils/util"), s = getApp().eventBus;

Component({
    properties: {
        showBtn: {
            type: Boolean,
            default: !1
        },
        query: {
            type: Object,
            default: {}
        }
    },
    data: {
        pageShow: !1,
        recommendNo: "",
        sign: "couponInfo.sign",
        send_coupon_params: [],
        send_coupon_merchant: "",
        appid: "",
        path: "",
        hasRec: !1,
        suggest_immediate_use: !0,
        stockExpireTime: 0
    },
    methods: {
        init: function() {
            this.sendcoupon();
        },
        toChooseNum: function(e) {
            var t = e.currentTarget.dataset.index, n = this.data.phoneList;
            n.forEach(function(e, n) {
                e.act = n === t;
            }), this.setData({
                phoneList: n
            }), this.setActNumber();
        },
        sendcoupon: function() {
            var e = this;
            return o(n().mark(function t() {
                var o, i, r, s, d;
                return n().wrap(function(t) {
                    for (;;) switch (t.prev = t.next) {
                      case 0:
                        return wx.showLoading({
                            title: "加载中..."
                        }), t.prev = 1, t.next = 4, a.default.sendcoupon();

                      case 4:
                        o = t.sent, wx.hideLoading(), i = o.code, r = o.data, s = o.message, d = {}, "0" === i ? (r.length > 0 && (d = JSON.parse(JSON.stringify(r[0]))) && d.recommendNo && (d.recommendNo = e.phoneAddGap(d.recommendNo)), 
                        e.setData({
                            recommendNo: d.recommendNo,
                            appid: d.appid,
                            path: d.path,
                            bgStyle: "background-image: url(" + "https://txwk.10010.com/miniprogram_kcard/inviteSendcoupon/bg".concat(d.recommendNo ? "_phone" : "", ".png") + ")",
                            btnStyle: "background-image: url(" + "https://txwk.10010.com/miniprogram_kcard/inviteSendcoupon/button".concat(d.stockExpireTime ? "" : "_dis", ".png") + ")",
                            sign: d.sign,
                            send_coupon_params: [ {
                                stock_id: d.stockId,
                                out_request_no: d.requestNo
                            } ],
                            send_coupon_merchant: d.merchantId,
                            stockExpireTime: d.stockExpireTime ? (0, c.dateFormat)(new Date(d.stockExpireTime), "YYYY-MM-DD hh:mm") : 0,
                            pageShow: !0
                        })) : e.showErrorDialog(s), t.next = 14;
                        break;

                      case 11:
                        t.prev = 11, t.t0 = t.catch(1), wx.hideLoading();

                      case 14:
                      case "end":
                        return t.stop();
                    }
                }, t, null, [ [ 1, 11 ] ]);
            }))();
        },
        getcoupon: function(e) {
            if (e && e.detail && e.detail.send_coupon_result) {
                var n = t(e.detail.send_coupon_result, 1)[0].code;
                "DUPREQUEST" !== n && "MAXQUOTA" !== n && "MAXQUOTA" !== n || this.setData({
                    hasRec: !0,
                    btnStyle: "background-image: url(https://txwk.10010.com/miniprogram_kcard/inviteSendcoupon/button_rec.png)"
                }), !e.detail.send_coupon_result || "DUPREQUEST" !== n && "MAXQUOTA" !== n || this.gotoActPage();
            }
        },
        gotoActPage: function() {
            "wxc47d9eb33eec8b6a" === this.data.appid ? wx.navigateTo({
                url: "".concat(0 !== this.data.path.indexOf("/") ? "/" : "").concat(this.data.path)
            }) : wx.navigateToMiniProgram({
                appId: this.data.appid,
                path: this.data.path
            });
        },
        userconfirm: function() {
            this.gotoActPage();
        },
        showErrorDialog: function(e) {
            i.default.alert({
                context: this,
                message: e || "网络错误"
            });
        },
        setActNumber: function() {
            var e = this.data.phoneList.find(function(e) {
                return e.act;
            });
            e && s.emit("inviteRecommend_getNumber", e.number);
        },
        gotoAppRule: function() {
            this.openWebview("https://txwk.10010.com/kcardorder/rules/index.html#/appRule");
        },
        gotoFeeRule: function() {
            this.openWebview("https://txwk.10010.com/wkpen/view/b44Dd7Af07.html");
        },
        openWebview: function(e) {
            wx.navigateTo({
                url: "/pages/webview/index?url=" + encodeURIComponent(e)
            });
        },
        clickInv: function() {
            r.default.clickStat("67734369d5a14c588a5f59643b4c60a6", {});
        },
        gotoOrder: function() {
            if (r.default.clickStat("fb50bae936bb4f13a458ad22690874ad", {}), this.data.finish || !this.data.phoneNum || this.data.errMsg) this.showErrorDialog(); else {
                var e = this.data.query, t = e.encData, n = e.productType, o = void 0 === n ? "k107" : n, a = e.channel, i = void 0 === a ? "01-3535-9999-9999" : a, s = this.data.phoneList.find(function(e) {
                    return e.act;
                }), d = "".concat("https://txwk.10010.com", "/kcardorder/gotoUid?state=KCARD_INVITE_RECOMMEND3_ORDERLAND&isSelf=1&").concat((0, 
                c.objToStr)({
                    productType: o,
                    encData: t,
                    channel: i,
                    ADTAG: i,
                    actNumber: s.number
                }));
                console.log(d), this.openWebview(d);
            }
        },
        phoneAddGap: function(e) {
            return e.replace(/(\d{3})(\d{4})/, "$1 $2 ");
        },
        formatPhone: function(e, t, n) {
            return [ e.substring(0, t), e.substring(t, t + n) ];
        }
    },
    created: function() {},
    attached: function() {
        this.init();
    },
    ready: function() {},
    moved: function() {},
    detached: function() {
        this.inviteRecommend_options && this.inviteRecommend_options.off();
    }
});